package com.hafu.swing;

import com.hafu.model.Course;
import com.hafu.service.CourseService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

// 选课面板
public class CourseSelectionPanel extends JPanel {
    private CourseService courseService;
    private String studentId;
    private DefaultTableModel tableModel;

    public CourseSelectionPanel(JPanel parentPanel, CourseService courseService, String studentId) {
        this.courseService = courseService;
        this.studentId = studentId;
        setLayout(new BorderLayout());

        // 顶部搜索栏
        JPanel searchPanel = new JPanel();
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("搜索");
        searchButton.addActionListener(e -> searchCourses(searchField.getText().trim()));
        searchPanel.add(new JLabel("搜索课程:"));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);
        add(searchPanel, BorderLayout.NORTH);

        // 表格数据
        String[] columnNames = {"课程ID", "课程名称", "授课教师", "学分", "课时", "已选人数", "状态", "操作"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return column == 7; // 只允许编辑最后一列(操作)
            }
        };
        JTable courseTable = new JTable(tableModel);
        courseTable.setRowHeight(30);
        JScrollPane scrollPane = new JScrollPane(courseTable);
        add(scrollPane, BorderLayout.CENTER);

        // 初始加载课程数据
        loadCourseData();
    }

    // 加载可选课程数据
    private void loadCourseData() {
        tableModel.setRowCount(0);
        List<Course> courses = courseService.getAvailableCoursesForStudent(studentId);
        for (Course course : courses) {
            Object[] rowData = {
                    course.getCourseId(),
                    course.getCourseName(),
                    getTeacherName(course.getTeacherId()),
                    course.getCredits(),
                    course.getHours(),
                    course.getEnrolledCount(),
                    course.getStatus(),
                    getOperationText(course)
            };
            tableModel.addRow(rowData);
        }
    }

    // 搜索课程
    private void searchCourses(String keyword) {
        tableModel.setRowCount(0);
        List<Course> courses = courseService.searchAvailableCoursesForStudent(studentId, keyword);
        for (Course course : courses) {
            Object[] rowData = {
                    course.getCourseId(),
                    course.getCourseName(),
                    getTeacherName(course.getTeacherId()),
                    course.getCredits(),
                    course.getHours(),
                    course.getEnrolledCount(),
                    course.getStatus(),
                    getOperationText(course)
            };
            tableModel.addRow(rowData);
        }
    }

    // 获取教师名称
    private String getTeacherName(String teacherId) {
        // 这里需要实现根据教师ID获取教师名称的逻辑
        return "教师名称";
    }

    // 获取操作文本
    private String getOperationText(Course course) {
        if ("已发布".equals(course.getStatus())) {
            return "选课";
        }
        return "无操作";
    }
}